package com.exmaple.springcloud.controller;

import cn.hutool.core.date.DateUtil;
import com.exmaple.springcloud.apis.PayFeignApi;
import com.exmaple.springcloud.domain.R;
import com.exmaple.springcloud.entity.PayDTO;
import com.exmaple.springcloud.enums.ReturnCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/consumer")
public class OrderController {
    @Autowired
    PayFeignApi payFeignApi;



    @PostMapping("/pay/add")
    public R addOrder(@RequestBody  PayDTO payDTO){
        System.out.println("第一步：模拟本地addOrder新增订单成功(省略sql操作) \n 第二步：再开启addPay支付微服务远程调用");
        return payFeignApi.addPay(payDTO);
        //return restTemplate.postForObject(PaymentSrv_URL+"/pay/add",payDTO,R.class);
    }


    @GetMapping("/pay/get/{id}")
    public R getPayInfo(@PathVariable("id") Integer id){
        System.out.println("-------支付微服务远程调用，按照id查询订单支付流水信息");
        R resultData = null;
        try
        {
            System.out.println("调用开始-----:"+ DateUtil.now());
            resultData = payFeignApi.getPayById(id);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("调用结束-----:"+DateUtil.now());
            return R.fail(ReturnCodeEnum.RC500.getCode(),e.getMessage());
        }
        return resultData;
        //return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/"+id, R.class, id);
    }



    @GetMapping(value = "/pay/get/info")
    public String getInfoByConsul(){
        return payFeignApi.getInfoByConsul();
        //return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/info", String.class);
    }

    @GetMapping(value = "/pay/get/serviceInfo")
    public String getPayServiceInfo(){
        return payFeignApi.getServiceInfo();
    }


}
